package myservlet.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mybean.data.login;

public class changeFixshop extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");	
			}
			catch(Exception e) {}
		}
	public  void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		//获取用户更改后的参数  
		String id = req.getParameter("id"),//参数名要与input里的name值对应
			tel= req.getParameter("tel"),//地区名
			addr= req.getParameter("addr");
		if(addr==""||tel=="") {
			out.println("<script language = javascript>alert('请完善信息!');");
			out.println("location.href='Ahomepage.jsp'</script>");
			return;
		}
		login loginBean =null; 
		HttpSession session= req.getSession(true);
		Connection con = null;
		try {
			loginBean = (login)session.getAttribute("loginBean");
			boolean b=loginBean==null || loginBean.getLogname()==null || loginBean.getLogname().length()==0;
			if(b) {
				out.println("<script language = javascript>alert('请重新登录!');");
				out.println("location.href='index.jsp'</script>");
				return;
			}
			String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8";
			PreparedStatement sql;
			ResultSet rs;
			//修改单车信息
			try {//连接数据库
				con = DriverManager.getConnection(url,"root","744615");
				String condition="update fixshop set Ftel=?,Faddr=? where Fno=?"; 
				sql=con.prepareStatement(condition);
				sql.setString(1, tel);  
				sql.setString(2, addr);
				sql.setString(3, id);
				sql.executeUpdate();  //执行更新操作，  更新数据库 
				out.println("<script language = javascript>alert('修改成功!');");
				out.println("location.href='Ahomepage.jsp'</script>");
			}
			catch (SQLException exp){
				System.out.println(exp);
				out.println("<script language = javascript>alert('修改失败!');");
				out.println("location.href='Ahomepage.jsp'</script>");
			}
			finally{ 
	            // 关闭Connection  
	            con.close();   
			}
		}
		catch (Exception exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('修改失败!');");
			out.println("location.href='Ahomepage.jsp'</script>");
		}
	}	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}


